package cn.edu.dlut.port.isp.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import cn.edu.dlut.port.isp.bean.Anchorage_info;
import cn.edu.dlut.port.isp.bean.Channel_info;
import cn.edu.dlut.port.isp.bean.M_thp_comp;
import cn.edu.dlut.port.isp.bean.M_thp_comp_full;
import cn.edu.dlut.port.isp.bean.M_thp_dlp;
import cn.edu.dlut.port.isp.bean.Port_info;
import cn.edu.dlut.port.isp.repostitory.M_thp_compRepository;
import cn.edu.dlut.port.isp.repostitory.M_thp_dlpRepository;

@Service
public class PortServiceImp implements PortService {
	@Autowired
	private JdbcTemplate jt;

	@Override
	public List<Anchorage_info> getAnchorageList() {
		List<Anchorage_info> list = jt.query("select * from Anchorage_info",
				new BeanPropertyRowMapper<Anchorage_info>(Anchorage_info.class));
		List<Port_info> list2 = jt.query("select * from Port_info",
				new BeanPropertyRowMapper<Port_info>(Port_info.class));
		for (int i = 0; i < list.size(); i++) {
			String p_s_names = "";
			String p_s_code = list.get(i).getP_s_code();// 获取服务港区的代码
			String[] p_s_codes = p_s_code.trim().split(";");
			for (String code : p_s_codes) {
				for (Port_info p : list2) {
					if (p.getP_a_code().toString().equals(code)) {
						if (p_s_names.length() == 0)
							p_s_names += p.getP_a_name();
						else
							p_s_names += ";" + p.getP_a_name();
					}

				}
			}
			list.get(i).setP_s_names(p_s_names);
		}
		return list;
	}

	@Override
	public List<M_thp_comp> getAllCompThpDate() {
		return jt.query("select distinct date from m_thp_comp",
				new BeanPropertyRowMapper<M_thp_comp>(M_thp_comp.class));
	}

	@Override
	public List<M_thp_comp> getAllCompThpYear() {
		return jt.query("select distinct floor(date/100) as date from m_thp_comp",
				new BeanPropertyRowMapper<M_thp_comp>(M_thp_comp.class));
	}

	@Override
	public List<M_thp_comp_full> getCompThpByYear(Integer year) {
		return jt.query("select sum(t.cargo_thp) as cargo_thp,t.comp_ID,c.comp_name,floor(t.Date/100) as date " + 
				" from m_thp_comp t ,comp_info c " + 
				" where floor(t.Date/100)=? and t.comp_id=c.comp_id " + 
				" group by t.comp_ID",new Object[] {year},
				new BeanPropertyRowMapper<M_thp_comp_full>(M_thp_comp_full.class));
		
	}

	@Override
	public List<M_thp_dlp> getAllDlpThpYear() {
		return jt.query("select distinct floor(date/100) as date from m_thp_dlp",
				new BeanPropertyRowMapper<M_thp_dlp>(M_thp_dlp.class));
	}

	
	@Autowired
	private M_thp_dlpRepository m_thp_dlpRepository;
	
	@Override
	public List<M_thp_dlp> getDlpThpByYear(Integer year) {
		List<M_thp_dlp> list=m_thp_dlpRepository.findThpByYear(year);
		M_thp_dlp sum=new M_thp_dlp();
		sum.setCargo_thp(0.0f);
		sum.setChemicals(0.0f);
		sum.setCoal(0.0f);
		sum.setConr_thp(0.0f);
		sum.setConr_weight(0.0f);
		sum.setCrude(0.0f);
		sum.setD_t_conr(0.0f);
		sum.setD_t_thp(0.0f);
		sum.setF_t_conr(0.0f);
		sum.setF_t_thp(0.0f);
		sum.setGrain(0.0f);
		sum.setIronstone(0.0f);
		sum.setMachineryE(0.0f);
		sum.setP_oil(0.0f);
		sum.setPger_thp(0.0f);
		sum.setRo_thp(0.0f);
		sum.setSteel(0.0f);			
		for(int i=0;i<list.size();i++)
		{			
				sum.setCargo_thp(sum.getCargo_thp()+list.get(i).getCargo_thp());
				sum.setChemicals(sum.getChemicals()+list.get(i).getChemicals());
				sum.setCoal(sum.getCoal()+list.get(i).getCoal());
				sum.setConr_thp(sum.getConr_thp()+list.get(i).getConr_thp());
				sum.setConr_weight(sum.getConr_weight()+list.get(i).getConr_weight());
				sum.setCrude(sum.getCrude()+list.get(i).getCrude());
				sum.setD_t_conr(sum.getD_t_conr()+list.get(i).getD_t_conr());
				sum.setD_t_thp(sum.getD_t_thp()+list.get(i).getD_t_thp());
				sum.setF_t_conr(sum.getF_t_conr()+list.get(i).getF_t_conr());
				sum.setF_t_thp(sum.getF_t_thp()+list.get(i).getF_t_thp());
				sum.setGrain(sum.getGrain()+list.get(i).getGrain());
				sum.setIronstone(sum.getIronstone()+list.get(i).getIronstone());
				sum.setMachineryE(sum.getMachineryE()+list.get(i).getMachineryE());
				sum.setP_oil(sum.getP_oil()+list.get(i).getP_oil());
				sum.setPger_thp(sum.getPger_thp()+list.get(i).getPger_thp());
				sum.setRo_thp(sum.getRo_thp()+list.get(i).getRo_thp());
				sum.setSteel(sum.getSteel()+list.get(i).getSteel());
		}
		sum.setDate(year);
		list.add(sum);
		return list;
	}
	@Autowired
	private M_thp_compRepository m_thp_compRepository;
	@Override
	public List<M_thp_comp> getThpByCompanyAndYear(String comp_id, Integer year) {
		List<M_thp_comp> list=m_thp_compRepository.findBycompID(comp_id);//根据公司选取所有的吞吐量数据
		List<M_thp_comp> result=new ArrayList<M_thp_comp>();
        
		float sum=0;
		M_thp_comp c=new M_thp_comp();
		c.setDate(year);
		c.setCompID(comp_id);
		c.setCargo_thp(sum);
		for(int i=0;i<list.size();i++)
		{
			int cdate=(int)(list.get(i).getDate()/100);
			if(cdate==year)
			{
				sum+=list.get(i).getCargo_thp();
				result.add(list.get(i));
			}
				
		}
		c.setCargo_thp(sum);
		result.add(c);
		System.out.println("----------------------"+result.toString());
		return result;
	}

	@Override
	public List<Channel_info> getChannelList() {
		List<Channel_info> list = jt.query("select * from Channel_info",
				new BeanPropertyRowMapper<Channel_info>(Channel_info.class));
		List<Port_info> list2 = jt.query("select * from Port_info",
				new BeanPropertyRowMapper<Port_info>(Port_info.class));
		for (int i = 0; i < list.size(); i++) {
			String p_s_names = "";
			String p_s_code = list.get(i).getP_s_code();// 获取服务港区的代码
			String[] p_s_codes = p_s_code.trim().split(";");
			for (String code : p_s_codes) {
				for (Port_info p : list2) {
					if (p.getP_a_code().toString().equals(code)) {
						if (p_s_names.length() == 0)
							p_s_names += p.getP_a_name();
						else
							p_s_names += ";" + p.getP_a_name();
					}

				}
			}
			list.get(i).setP_s_names(p_s_names);
		}
		return list;
	}
}
